package cn.iocoder.an.module.business.controller.admin.equipmentkeepsetting;

import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.EquipmentImportExcelVO;
import cn.iocoder.an.module.business.controller.admin.equipmentmanage.vo.ImportRespVO;
import cn.iocoder.an.module.business.enums.EquipmentStatusEnum;
import cn.iocoder.an.module.business.enums.EquipmentTypeEnum;
import cn.iocoder.an.module.business.enums.EquipmentUsedEnum;
import cn.iocoder.an.module.business.enums.MaintenanceStatusEnum;
import com.fhs.core.trans.anno.TransMethodResult;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.business.controller.admin.equipmentkeepsetting.vo.*;
import cn.iocoder.an.module.business.dal.dataobject.equipmentkeepsetting.EquipmentKeepSettingDO;
import cn.iocoder.an.module.business.service.equipmentkeepsetting.EquipmentKeepSettingService;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "管理后台 - 保养项设置")
@RestController
@RequestMapping("/business/equipment-keep-setting")
@Validated
public class EquipmentKeepSettingController {

    @Resource
    private EquipmentKeepSettingService equipmentKeepSettingService;

    @PostMapping("/create")
    @Operation(summary = "创建保养项设置")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:create')")
    public CommonResult<Long> createEquipmentKeepSetting(@Valid @RequestBody EquipmentKeepSettingSaveReqVO createReqVO) {
        return success(equipmentKeepSettingService.createEquipmentKeepSetting(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新保养项设置")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:update')")
    public CommonResult<Boolean> updateEquipmentKeepSetting(@Valid @RequestBody EquipmentKeepSettingSaveReqVO updateReqVO) {
        equipmentKeepSettingService.updateEquipmentKeepSetting(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除保养项设置")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:delete')")
    public CommonResult<Boolean> deleteEquipmentKeepSetting(@RequestParam("id") Long id) {
        equipmentKeepSettingService.deleteEquipmentKeepSetting(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除保养项设置")
                @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:delete')")
    public CommonResult<Boolean> deleteEquipmentKeepSettingList(@RequestParam("ids") List<Long> ids) {
        equipmentKeepSettingService.deleteEquipmentKeepSettingListByIds(ids);
        return success(true);
    }

    @GetMapping("/id-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量获取保养项设置")
    @TransMethodResult
    public CommonResult<List<EquipmentKeepSettingRespVO>> getEquipmentKeepSettingListByIds(@RequestParam("ids") List<Long> ids) {
        return success(equipmentKeepSettingService.getEquipmentKeepSettingListByIds(ids));
    }

    @GetMapping("/get")
    @Operation(summary = "获得保养项设置")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:query')")
    @TransMethodResult
    public CommonResult<EquipmentKeepSettingRespVO> getEquipmentKeepSetting(@RequestParam("id") Long id) {
        EquipmentKeepSettingDO equipmentKeepSetting = equipmentKeepSettingService.getEquipmentKeepSetting(id);
        return success(BeanUtils.toBean(equipmentKeepSetting, EquipmentKeepSettingRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得保养项设置分页")
    @TransMethodResult
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:query')")
    public CommonResult<PageResult<EquipmentKeepSettingRespVO>> getEquipmentKeepSettingPage(@Valid EquipmentKeepSettingPageReqVO pageReqVO) {
        PageResult<EquipmentKeepSettingDO> pageResult = equipmentKeepSettingService.getEquipmentKeepSettingPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentKeepSettingRespVO.class));
    }

    @GetMapping("/pageNew")
    @Operation(summary = "获得保养项设置分页")
    @TransMethodResult
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:query')")
    public CommonResult<PageResult<EquipmentKeepSettingRespVO>> getEquipmentKeepSettingPageNew(@Valid EquipmentKeepSettingPageReqVO pageReqVO) {
        PageResult<EquipmentKeepSettingDO> pageResult = equipmentKeepSettingService.getEquipmentKeepSettingPageNew(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EquipmentKeepSettingRespVO.class));
    }


    @GetMapping("/allList")
    @Operation(summary = "获得保养项设置")
    @TransMethodResult
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:query')")
    public CommonResult<List<EquipmentKeepSettingRespVO>> getEquipmentKeepSettingAllList(@Valid EquipmentKeepSettingPageReqVO pageReqVO) {
        List<EquipmentKeepSettingDO> list = equipmentKeepSettingService.getEquipmentKeepSettingAllList(pageReqVO);
        return success(BeanUtils.toBean(list, EquipmentKeepSettingRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出保养项设置 Excel")
    @PreAuthorize("@ss.hasPermission('business:equipment-keep-setting:export')")
    @ApiAccessLog(operateType = EXPORT)
    @TransMethodResult
    public void exportEquipmentKeepSettingExcel(@Valid EquipmentKeepSettingPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EquipmentKeepSettingDO> list = equipmentKeepSettingService.getEquipmentKeepSettingPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "保养项设置.xlsx", "数据", EquipmentKeepSettingRespVO.class,
                        BeanUtils.toBean(list, EquipmentKeepSettingRespVO.class));
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得保养设置导入模板导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<EquipmentSettingImportExcelVO> list = Arrays.asList(
                EquipmentSettingImportExcelVO.builder().keepTypeName("xx保养").name("1.检测各部位有无泄露").keepTypeName("泄漏检测")
                        .status(MaintenanceStatusEnum.run.getStatus()).keepNorm("1.必须达到表面干净").description("xxxxx描述").keepMethod("1.使用xxx进行保养").build()
        );
        // 输出
        ExcelUtils.write(response, "设备台账导入模板.xlsx", "设备台账列表", EquipmentSettingImportExcelVO.class, list);
    }


    @PostMapping("/import")
    @Operation(summary = "导入")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public CommonResult<ImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                  @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport) throws Exception {
        List<EquipmentSettingImportExcelVO> list = ExcelUtils.read(file, EquipmentSettingImportExcelVO.class);
        return success(equipmentKeepSettingService.importList(list, updateSupport));
    }

}